package com.raigor.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.DatatypeConverter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * SHA加密方法
 *
 * @author libaolu
 */
public class SHAUtils {
    private static final Logger log = LoggerFactory.getLogger(SHAUtils.class);
    public static String sha256(Long userId){
        MessageDigest algorithm;
        try {
            algorithm = MessageDigest.getInstance("SHA-256");
            algorithm.update((String.valueOf(userId) + System.currentTimeMillis()).getBytes());
            return DatatypeConverter.printBase64Binary(algorithm.digest());
        } catch (NoSuchAlgorithmException e) {
            log.error("SHA-256 Error...", e);
        }
        return null;
    }
}
